package com.camel.gateway.config;

import cn.dev33.satoken.dao.SaTokenDao;
import cn.dev33.satoken.stp.StpInterface;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author camel
 */
@Component
public class StpInterfaceImpl implements StpInterface {

    @Autowired
    private SaTokenDao saTokenDao;

    @Override
    public List<String> getPermissionList(Object o, String s) {
        String s1 = saTokenDao.get("x-token:login:permission:" + StpUtil.getTokenValueByLoginId(o));
        // 返回此 loginId 拥有的权限列表
        if(StringUtils.isEmpty(s1)) {
            return new ArrayList<>();
        }
        return CollectionUtils.arrayToList(s1.split(","));
    }

    @Override
    public List<String> getRoleList(Object o, String s) {
        // 返回此 loginId 拥有的角色列表
        String s1 = saTokenDao.get("x-token:login:role:" + StpUtil.getTokenValueByLoginId(o));
        if(StringUtils.isEmpty(s1)) {
            return new ArrayList<>();
        }
        return CollectionUtils.arrayToList(s1.split(","));
    }
}
